package com.cuit.view;

import com.cuit.domain.User;
import com.cuit.domain.ZhangWu;
import com.cuit.service.ZhangWuService;

import java.util.Date;
import java.util.List;
import java.util.Scanner;

public class MainView {
    // 本项目view依赖于service
    private ZhangWuService zhangWuService = new ZhangWuService();

    private User user;

    MainView(User user) {
        this.user = user;
    }

    // 运行方法
    void run() {
        boolean flag = true;
        Scanner sc = new Scanner(System.in);
        while (flag) {
            System.out.println("--------------------记账系统--------------------");
            System.out.println("当前用户：" + user.getUsername());
            System.out.println("1. 添加账务 2. 编辑账务 3. 删除账务 4. 查询账务 5. 搜索账务 6. 退出账户");
            System.out.println("请输入要操作的功能号[1-6]...");
            int num = sc.nextInt();
            switch (num) {
                case 1:
                    addZhangWu();
                    break;
                case 2:
                    editZhangWu();
                    break;
                case 3:
                    deleteZhangWu();
                    break;
                case 4:
                    selectZhangWu();
                    break;
                case 5:
                    searchZhangWu();
                    break;
                case 6:
                    System.out.println("欢迎下次使用！");
                    flag = false;
                    break;
                default:
                    System.out.println("输入有误");
                    break;
            }
        }
    }

    private void addZhangWu() {
        Scanner sc = new Scanner(System.in);
        ZhangWu zhangWu = new ZhangWu();
        System.out.println("请输入类别：");
        zhangWu.setFlname(sc.nextLine());
        System.out.println("请输入金额：");
        zhangWu.setMoney(Double.parseDouble(sc.nextLine()));
        System.out.println("请输入账户：");
        zhangWu.setZhanghu(sc.nextLine());
        System.out.println("请输入时间：");
        zhangWu.setCreatetime(sc.nextLine());
        System.out.println("请输入说明：");
        zhangWu.setDescription(sc.nextLine());

        int i = zhangWuService.addZhangWu(zhangWu);
        if (i == 1) {
            System.out.println("添加成功！");
        } else {
            System.out.println("添加失败！");
        }
    }

    private void editZhangWu() {
        Scanner sc = new Scanner(System.in);
        ZhangWu zhangWu = new ZhangWu();
        System.out.println("请输入要修改的账务id：");
        zhangWu.setZwid(Integer.parseInt(sc.nextLine()));
        System.out.println("请输入类别：");
        zhangWu.setFlname(sc.nextLine());
        System.out.println("请输入金额：");
        zhangWu.setMoney(Double.parseDouble(sc.nextLine()));
        System.out.println("请输入账户：");
        zhangWu.setZhanghu(sc.nextLine());
        System.out.println("请输入时间：");
        zhangWu.setCreatetime(sc.nextLine());
        System.out.println("请输入说明：");
        zhangWu.setDescription(sc.nextLine());

        int i = zhangWuService.editZhangWu(zhangWu);
        if (i == 1) {
            System.out.println("编辑成功！");
        } else {
            System.out.println("编辑失败！");
        }
    }

    private void deleteZhangWu() {
        System.out.println("请输入要删除的id：");
        Scanner sc = new Scanner(System.in);
        int i = zhangWuService.deleteZhangWu(Integer.parseInt(sc.nextLine()));
        if (i == 1) {
            System.out.println("删除成功！");
        } else {
            System.out.println("删除失败！");
        }
    }

    private void selectZhangWu() {
        System.out.println("1. 查询所有 2. 按条件查询");
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        switch (num) {
            case 1:
                selectAll();
                break;
            case 2:
                selectByCreatTime();
                break;
            default:
                System.out.println("输入有误");
                break;
        }
    }

    private void selectAll() {
        List<ZhangWu> zwList = zhangWuService.selectAll();
        print(zwList);
    }

    private void selectByCreatTime() {
        Scanner sc = new Scanner(System.in);
        System.out.println("起始时间：");
        String sDate = sc.nextLine();
        System.out.println("结束时间：");
        String eDate = sc.nextLine();
        List<ZhangWu> zwList = zhangWuService.selectByCreatTime(sDate, eDate);
        print(zwList);
    }

    private void searchZhangWu() {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入账务：");
        String zw = sc.nextLine();
        List<ZhangWu> zwList = zhangWuService.searchZhangWu(zw);
        print(zwList);
    }

    private void print(List<ZhangWu> zwList) {
        System.out.println("ID\t\t类别\t\t金额\t\t账户\t\t时间\t\t说明");
        for (ZhangWu zw : zwList) {
            System.out.println(
                    zw.getZwid() + "\t\t" +
                            zw.getFlname() + "\t\t" +
                            zw.getMoney() + "\t\t" +
                            zw.getZhanghu() + "\t\t" +
                            zw.getCreatetime() + "\t\t" +
                            zw.getDescription() + "\t\t");
        }
    }

}
